In this lecture, we're going to be scripting the first trap of our RB, which are wooden beams that
break shortly after standing on them.
So let's go ahead and get started.
So we're going to create a new module script inside of the handler section for our starter player script.
And I'm going to name this our would break handler.
And this script is going to go through and grab all of the different modules in our game that are tagged
with the wood break tag.
And then we're going to use a class to create objects for those different models.
So I'm going to use the same template that we've been using before.
And I'm going to rename it to Wood Break Handler.
And what we're going to need is an initialize function, because we're going to be listening to the
collection service.
So we'll get the collection service game, get service collection service.
And we'll create a constant to store the tag that we're going to be tracking, which is the tag of would
break.
And inside of our initialize function, we're going to refer to the collection service and get instance
added signal for this tag.
And we're going to connect a function to this get that instance.
And then we're going to use a class to create an object for this model.
So we'll create another module script as a child of our handler script.
And we're going to rename this one to be our breakable wood class.
And then I'm going to be using another template which is going to be attached to this lecture.
And this is going to be my class template.
So it's a very simple template that basically just sets up our constructor for us as well as our destroy
function.
And then if we attempt to create an object again on the exact same model, we'll just return that object
that already exists in case we find it within our cache up here.
So we're going to rename this to our breakable wood class.
And then we can go ahead and rename this to Breakable Wood Objects.
And then we can go ahead and require this class from our handler.
So we'll call it breakable wood class.
And that's going to be equal to script dot breakable wood class.
And then every single time an instance is added in our game with this tag, we can go ahead and refer
to our breakable wood class and use the new constructor and pass our instance.
So inside of this class, what we're going to need are several different services.
We're going to need the player service to listen for when the player touches one of the parts.
We're going to need the sound service to play the wood breaking sound.
We're going to need the tween service to fade out or fade in our wooden beams.
And then we're also going to need a replicated storage to be able to grab the particle emitter that's
going to be emitting those wood particles.
So we'll get the player service.
We'll go ahead and get the sound service.
We'll also get replicated storage.
And then we're also going to get the tween service.
Now, when the start new constructor is going to be called, we're going to get passed a model and we'll
store a reference of that model inside of our object.
Next, we can go ahead and store a value that's going to represent the delay before our part breaks.
So when the player touches it we're going to wait for this delay period.
And then we're going to destroy the wooden beam.
So we'll call this delay.
And it's going to be equal to the model.
And I have an attribute on the model called delay.
Just in case our model does not have this attribute on it, then we can go ahead and set a default delay,
and we can go ahead and define that as a constant inside of our variable section.
And we can go ahead and call it default delay.
And the default delay can be like 0.5 seconds.
And then we'll pass that here.
So now either the delay is going to be defined on the model or we'll have a default delay.
And then we can also go ahead and have a boolean to represent a dip out.
So we'll call it on cooldown.
And it's going to be false by default.
So if we actually go ahead and take a look at all of our RB models, let me go ahead and parent these
to our workspace.
Here are the different wooden beams or these different brake parts that are going to be, um, destroying
themselves when a player touches them.
So whenever a player touches this invisible detect part, then we're going to wait for that delay.
And then we're going to make this part, uh, non collidable and spit out those particles just to see
if we can try and kill the player by having them falling through the map.
But each of these brake parts have an attribute set for the delay.
I've set 0.3 seconds for these brake parts, so every single one of these that get generated on the
map are going to have a delay of 0.3 seconds.
But just in case we accidentally forget to set a delay, then the default is going to be 0.5 seconds.
And you can also see that each one of these models has that tag of wood brake.
So what we want to do is we want to get our model and get that detect zone part and listen for when
it is touched.
And we can go ahead and connect a lambda function to this and get that part that has touched our detect
zone.
And we want to check if a player has touched this detect zone.
So we're going to use the player service and the function get player from character and pass the parent
of other part.
If there is no player, then we're just going to return.
Otherwise we can go ahead and break and we can go ahead and define two functions directly on our object.
One can be break to break the beam, and then we can have another function.
We could call it uh restore to restore the beam back to its original state.
So when we touch this part then we can go ahead and call our self.
Uh, brake function.
And then we also want to go ahead and listen for when our model is being destroyed.
And when that happens, then we can go ahead and call the destroy function on our object.
That way we can go ahead and clean everything up and not leave anything sitting around in memory.
Okay, so inside of the break function, what we want to do is we want to break the part.
We want to emit particles.
We want to set the can collide property to false and then make the part invisible.
However, we don't want to break the part multiple times.
So that's what that on cooldown debounce is going to be for.
Since when the part is going to be touched, that touched event is going to be fired multiple times.
We need to have a debounce.
So if we are already broken or we're on a cooldown now, we're just going to return.
Otherwise we're going to set on cooldown equal to true.
And then what we need to do is we need to wait for that delay.
So we'll wait for self dot delay before we break the part.
And then once we do that, we're going to go ahead and play the breaking sound.
And we can grab that from the sound service.
So up here we can make some references to the break sound.
So break sound is going to be equal to sound service dot SFX.
And inside of there there's a sound called Wood Break.
Actually let me reposition it up here.
And then we can also go ahead and get the particle emitter from Replicated storage.
So replicated storage dot assets dot wood break particle emitter.
And then we can define a couple more constants.
For example how many particles we would like to emit from the particle emitter.
We could call this particle emission.
And we could set it to a value like 50.
And then we can also define a constant for how long it will take for the plank to respawn.
So we can call this a respawn duration and set it to something like eight seconds.
So I'm back inside of our break function.
What we could do is we can create a clone of that break sound.
We'll set the player on, remove property equal to true, and then set the parent of the clone equal
to the self.
Dot model dot break part.
And then we can just destroy the clone to play the sound.
And then afterwards we'll get a clone of the emitter, so we'll call it emitter clone.
Emitter clone.
We'll set the parent equal to self dot model dot break part.
And then we can go ahead and emit.
The particle emission amount out of our particle emitter.
Then afterwards we're going to set the break part transparency equal to one.
So it's invisible.
And then we'll just copy this.
And this time we're going to update the Kankali property to be false.
And then afterwards, what we could do is we could have a wait statement to wait for the emitter clone's
particles to all disappear.
So we refer to emitter clone, we get the lifetime property and then get the max number in that range.
Then we can go ahead and destroy that emitter clone because we don't need it anymore.
And then after we have broken the part, then we want to go ahead and wait for that delay and then restore
the part back to its original state.
So before we wait down here for the particle emitter, we can use task dot delay to delay a function
from occurring.
And we're going to wait for that respawn duration.
And the function we're going to be calling is the self restore function.
Now since we're using a colon here, but we want to pass the function to our task dot delay function.
We're going to index with restore.
And then we need to pass self to the restore function as you know the self parameter.
And then inside of our restore function, what we can go ahead and do is we first need to check whether
or not this model has been destroyed.
So if we don't have a reference to the model.
Or if we do have a reference to the model, but the parent is equal to nil.
Or let's say we cannot find inside of our model the break part for some reason.
Then we're just going to return because we're yielding for those eight seconds.
The player could have walked far enough away where the entire model gets deleted.
So we want to make sure that we don't try or attempt to restore this object that's already been deleted.
We're just going to return.
Otherwise, what we could go ahead and do is we could use the tween service to fade that beam back to
its original state.
So we'll get the break part.
We'll do a tween info of, for example, one second and we want to update the transparency equal to
zero to be fully opaque.
We'll play that tween.
We'll set self dot model dot break.
Part dot can collide back to true.
And then we can go ahead and set self dot on cooldown equal to false.
Once this tween finishes playing.
So actually we'll get the tween.
From the Tween Service.
And then we can go ahead and play the tween.
And then when that twin completes.
Then we can go ahead and set self on cooldown equal to false.
Okay, that was a lot of code to write, so we should have everything set up we need for our, uh,
breakable wood trap.
So let's go ahead and test it out.
And before we do that, we need to move all of these Obby models back into replicated storage.
So we'll get all those children, and then we'll go ahead and set the parent back to the Obby models
folder.
And now we can go ahead and play test.
And perfect.
The first ones we got here are the.
Uh, bridges.
So if we go and step on this, there we go.
It breaks.
It has disappeared.
And then if we wait eight seconds, it should fade back and come to its original state.
So we're just going to sit here and wait.
Hopefully it'll fade in.
And unfortunately it does not look like it's restoring itself.
So let's go ahead and see what we did wrong.
Exactly.
So perhaps maybe it doesn't like us doing this.
So maybe an alternative.
What we could do is we could just pass our function here and then just call self Restore instead.
Maybe that'll work.
Um, yeah, I'll just try it out.
So there we go.
We get our bridge.
If we touch it, it breaks.
And then let's go ahead and see if it restores itself.
And unfortunately still not restoring itself so there could be an issue.
A respawn duration is set to eight seconds, so it shouldn't be taking that long, but it appears that
our restore function is not actually executing, and it appears that there could be an issue with this
return statement right here.
It could be returning prematurely for some reason.
So if there isn't a model or the parent is equal to nil.
Or.
Oh, that's why we forgot to include a Not statement here.
If there isn't a break part because we had the not not there, it found the break part and it was returning.
So we got to put that not statement there.
That means we should be able to restore this back to self restore and then pass self.
So let's go ahead and try that one more time.
Forget our wood bridge over there.
We'll go walk over to it.
And then we go and step on it, breaks it, and then it should restore itself.
Any second now.
And there we go.
It fades back in.
And then if we touch it again, as you can see, it breaks again.
And then we can walk along this bridge.
And that's pretty neat.
So now we have this trap where when we walk along the wooden beams there's a small little delay.
And then afterwards all the beams break.
So that means if you were to stand on one a little too long, as you can see, they break and you fall
to your death.
Very cool.
So that's the first trap completed thus far.
Keep up the good work and I'll see you in the next lecture.